package com.baizhi.lql.service;

import com.baizhi.lql.dao.CategoryMapper;
import com.baizhi.lql.entity.Category;
import com.baizhi.lql.entity.CategoryExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("categoryService")
@Transactional()
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categorydao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryTwoPage(int page, int pageSize,int categoryId) {
        Map<String,Object> map = new HashMap<>();

        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdIsNotNull().andParentIdEqualTo(categoryId);
        int offset = (page - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<Category> categoryList = categorydao.selectByExampleAndRowBounds(example, rowBounds);
        int count = categorydao.selectCount(null);

        map.put("rows",categoryList);
        map.put("total",count);
        map.put("page",page);

        return map;
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryPage(int page, int pageSize) {
        Map<String,Object> map = new HashMap<>();

        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdIsNull();
        int offset = (page - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);

        List<Category> categories = categorydao.selectByExampleAndRowBounds(example, rowBounds);
        int count = categorydao.selectCount(null);

        map.put("rows",categories);
        map.put("total",count);
        map.put("page",page);

        return map;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Category queryOne(int id) {
        return categorydao.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Category category) {
        categorydao.updateByPrimaryKey(category);
    }


    @Override
    public Map<String,String> remove(int id,int parentId) {
        Map<String, String> map = new HashMap<>();
       /* CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        if(parentId == null){ //判断删除一级
            criteria.andLevelsEqualTo(1);
            if(){
                criteria.andIdEqualTo(id);
            }else { // 有 错误提示
                map.put("message", "一级类别下有二级类别不能删除");
                return map;
            }
        }else { // 判断二级删除

        }
        map.put("message","删除成功");
        categorydao.deleteByPrimaryKey(id);*/
        return map;
    }

    @Override
    public void reg(Category category) {
        if(category.getParentId() == null){     // 一级添加
            category.setLevels(1);
        }else {         // 二级添加
            category.setLevels(2);
        }
        categorydao.insertSelective(category);
    }

    @Override
    public List<Category> queryByLevelsCategory() {
        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdIsNull();   // parentId 为空
        List<Category> categories = categorydao.selectByExample(example);  //条件查询
        return categories;
    }
}
